package com.rocksea.app.dialog;

import android.content.Context;
import android.support.design.widget.BottomSheetDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import com.rocksea.app.R;

/**
 * Created by 吴志华 on 2017/10/17.
 *
 *
 */

public class CustomBottomSheetDialog
{

    private OnBottomSheetDialogListener onBottomSheetDialogListener;
    private Context context;
    private BottomSheetDialog bottomSheetDialog;

    public CustomBottomSheetDialog(Context context, OnBottomSheetDialogListener onBottomSheetDialogListener)
    {
        this.context = context;
        this.onBottomSheetDialogListener = onBottomSheetDialogListener;
        showBottomSheetDialog();
    }

    public void showDialog()
    {
        if (bottomSheetDialog != null)
            bottomSheetDialog.show();
    }

    private void showBottomSheetDialog()
    {
        bottomSheetDialog = new BottomSheetDialog(context);
        View dialogView = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_dialog_layout,null);
        dialogView.findViewById(R.id.Take_Photo).setOnClickListener(onBottomSheetDialogClick);
        dialogView.findViewById(R.id.Photo_ALbum).setOnClickListener(onBottomSheetDialogClick);
        dialogView.findViewById(R.id.Cancel).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                bottomSheetDialog.dismiss();
            }
        });
        bottomSheetDialog.setContentView(dialogView);
        bottomSheetDialog.setCancelable(true);
    }

    public interface OnBottomSheetDialogListener
    {
        void onTakePhoto();
        void onPhotoALbum();
    }

    private AdapterView.OnClickListener onBottomSheetDialogClick = new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            int i = v.getId();
            if (i == R.id.Take_Photo)
            {
                if (bottomSheetDialog != null)
                    bottomSheetDialog.dismiss();
                if (onBottomSheetDialogListener !=null)
                    onBottomSheetDialogListener.onTakePhoto();
            }
            else if (i == R.id.Photo_ALbum)
            {
                if (bottomSheetDialog != null)
                    bottomSheetDialog.dismiss();
                if (onBottomSheetDialogListener !=null)
                    onBottomSheetDialogListener.onPhotoALbum();
            }
        }
    };
}
